#!usr/bin/python
# -*= coding: UTF-8 -*-
'''
练习
写一个矩形类，默认有宽和高两个属性
如果为一个叫square的属性赋值，那么说明这是一个正方形，值就是正方形的边长，
此时宽和高都应该等于边长
'''
class Rectangle:
    def __init__(self, width = 8, height = 8):
        self.width = width
        self.height = height

    def __setattr__(self, name, value):
        if name == 'square':
            self.width = value
            self.height = value
        else:
            #self.name = value 这样子写有毛病，无限递归死循环
            #super().__setattr__(name, value)
            self.__dict__[name] = value

    def getArea(self):
        return  self.width * self.height

r1 = Rectangle(4, 5)
print(r1)

print(r1.getArea())

r1.square = 10
print(r1.width)
print(r1.height)
print(r1.getArea())
print(r1.__dict__)

r1 = Rectangle(4, 5)
print('now', r1.width)
print(r1.height)
print(r1.getArea())
print(r1.__dict__)





